import mysql from "mysql";
import tsql from "ts-sql-plugin/sql";

let mysql_config = {
  // debug: true,
  // connectionLimit: 10,
  host: "127.0.0.1",
  port: 32771,
  user: "root",
  password: "root",
  database: "sts_traffic_portal",
};

export const pool = mysql.createPool(mysql_config);

type SqlFn = ((...args: any[]) => Promise<any>) & typeof tsql;
export const sql = ((...args: any[]) => {
  const query = (tsql as any)(...args);
  const ms_query = {
    sql: query.text.replace(/\?\?/g, "?"),
    values: query.values,
  };
  return new Promise<any>((res, rej) => pool.query(ms_query, (err, result, fields) => (err ? rej(err) : res(result))));
}) as SqlFn;
Object.assign(sql, tsql);

export default sql;
